package com.cnswhy.cloud.server.comsumer.consumer.split;

import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.SystemClockUtil;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.common.entity.enumeration.TransType;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.common.entity.mq.CpsOrderQueryMQ;
import com.cnswhy.cloud.server.comsumer.event.CpsOrderSplitEvent;
import com.cnswhy.cloud.server.core.entity.CpsTransOrder;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerWallet;
import com.cnswhy.cloud.server.core.entity.WalletTransDtl;
import com.cnswhy.cloud.server.core.oem.SystemService;
import com.cnswhy.cloud.server.core.service.CpsTransOrderService;
import com.cnswhy.cloud.server.core.service.WalletTransDtlService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.wallet.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 3. 新注册客户完成首次快捷收款，得奖励8元；
 */
@Component
@Slf4j
public class FirstPayRewordConsumer implements CpsOrderSplitEvent {
    @Autowired
    CpsTransOrderService cpsTransOrderService;
    @Autowired
    WalletService walletService;
    @Autowired
    SystemService systemService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    WalletTransDtlService walletTransDtlService;
    @Autowired
    CustomerService customerService;

    @Override
    public void split(CpsOrderQueryMQ cpsOrderQueryMQ) throws BizException {
        log.info("分账订单：{}", cpsOrderQueryMQ.getCpsId());
        CpsTransOrder cpsTransOrder = cpsTransOrderService.getById(cpsOrderQueryMQ.getCpsId());
        if (cpsTransOrder == null) {
            log.error("分账订单-无效CPS_TRANS_ORDER_ID:{}", cpsOrderQueryMQ.getCpsId());
            return;
        }
        Long firstQuickPayReword = systemService.getQuickPayReword();
        if (firstQuickPayReword.compareTo(0L) <= 0) {
            return;
        }
        List<CpsTransOrder> cpsTransOrderList = cpsTransOrderService.list(Wraps.<CpsTransOrder>lbQ()
                .eq(CpsTransOrder::getCustomerId, cpsTransOrder.getCustomerId())
                .eq(CpsTransOrder::getStatus, 2));
        if (cpsTransOrderList.size() > 1) {
            return;
        }
        Customer customer = customerService.getById(cpsTransOrder.getCustomerId());
        customer.setFirstPay(1);
        customer.setFirstPayTime(SystemClockUtil.getLocalDateTime());
        customerService.updateById(customer);
        CustomerWallet customerWallet = walletService.checkAndCreate(cpsTransOrder.getCustomerId(), WalletType.REWORD, 0L);
        List<WalletTransDtl> walletTransDtlList = walletTransDtlService.list(
                Wraps.<WalletTransDtl>lbQ().eq(WalletTransDtl::getTransType,TransType.FIRST_NO_CARD_PAY)
                .eq(WalletTransDtl::getCustomerWalletId, customerWallet.getId())
        );
        if(walletTransDtlList.size() >= 1){
            return ;
        }
        walletService.add(customerWallet.getCustomerId(), WalletType.REWORD, TransType.FIRST_NO_CARD_PAY, firstQuickPayReword, idGeneratorService.genCommonIdString());
    }

}
